/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.manto;

import com.uca.sisbec.commons.HibernateAction;
import com.uca.sisbec.commons.Utils;
import com.uca.sisbec.forms.manto.UsuarioForm;
import com.uca.sisbec.forms.usuario.ChangePassForm;
import com.uca.sisbec.helpers.manto.UsuarioHelper;
import com.uca.sisbec.mapping.Usuario;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;

/**
 *
 * @author Juank
 */
public class UsuarioAction extends HibernateAction {
    public ActionForward init(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        UsuarioForm uf = (UsuarioForm) form;
        request.setAttribute("save", "new");
        uf.setEstado(true);
        setOptionsList(request);
        return mapping.findForward("success");
    }

    public ActionForward initBusqueda(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        setOptionsList(request);
        return mapping.findForward("search");
    }

    public ActionForward agregarUsuario(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        UsuarioForm uf = (UsuarioForm) form;
        ActionMessages msg = new ActionMessages();
        ActionErrors errors = new ActionErrors();
        UsuarioHelper helper = new UsuarioHelper(getSession(request));
        if (helper.validateUsuario(uf)){
            helper.insertarUsuario(uf);
            msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("usuario.nuevo"));
            request.setAttribute("msg", msg);
            request.setAttribute("save", "update");
        }else{
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("usuario.existe", uf.getUser()));
            request.setAttribute("errors", errors);
            request.setAttribute("save", "new");
        }
        setOptionsList(request);
        return mapping.findForward("success");
    }
    
    public ActionForward modificarUsuario(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        UsuarioForm uf = (UsuarioForm) form;
        UsuarioHelper helper = new UsuarioHelper(getSession(request));
        helper.modificarUsuario(uf);
        ActionMessages msg = new ActionMessages();
        msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("usuario.modificado"));
        request.setAttribute("msg", msg);
        request.setAttribute("save", "update");
        setOptionsList(request);
        return mapping.findForward("success");
    }

    public ActionForward buscarUsuario(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        UsuarioForm uf = (UsuarioForm) form;
        UsuarioHelper helper = new UsuarioHelper(getSession(request));
        request.setAttribute("usuarioData", helper.buscarUsuarios(uf));
        setOptionsList(request);
        return mapping.findForward("search");
    }

    public ActionForward seleccionarUsuario(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        UsuarioForm uf = (UsuarioForm) form;
        UsuarioHelper helper = new UsuarioHelper(getSession(request));
        String user = uf.getUser();
        helper.buscarUsuarioToForm(user, uf);
        request.setAttribute("save", "update");
        setOptionsList(request);
        return mapping.findForward("success");
    }
    
    public void setOptionsList(HttpServletRequest request){
        UsuarioHelper helper = new UsuarioHelper(getSession(request));
        request.setAttribute("rolesList", helper.getRoles());
    }
}
